//
//  ZTBaseVideoTableViewController.m
//  zhibo8
//
//  Created by zzt on 16/11/21.
//  Copyright © 2016年 zzt. All rights reserved.
//

#import "ZTBaseVideoTableViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import <MJRefresh/MJRefresh.h>
#import "ZTVideoICelltem.h"
#import "ZTVideoCell.h"
#import "ZTFootNewsTableViewController.h"
#import "ZTNBANewsTableViewController.h"

#import "ZTCareNewsTableViewController.h"

@interface ZTBaseVideoTableViewController ()
@property (nonatomic, strong)NSMutableArray *modelArray;
@property (nonatomic, strong) AFHTTPSessionManager *mgr;
@end


static NSString * const ID = @"cell";

@implementation ZTBaseVideoTableViewController

- (AFHTTPSessionManager *)mgr
{
    if (_mgr == nil) {
        _mgr = [AFHTTPSessionManager manager];
        
        // 告诉AFN，支持接受 text/xml 的数据
        _mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    }
    return _mgr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.加载数据
//    [self loadNewData];
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"ZTVideoCell" bundle:nil] forCellReuseIdentifier:ID];
    
    self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
    
    // 添加上下拉刷新s
    [self setupRefreshView];
    
    // 请求更新数据
    [self.tableView.mj_header beginRefreshing];
    
    // 监听一次
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reload) name:@"reloadTheme" object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 添加上下拉刷新
- (void)setupRefreshView
{
    // 请求成功,一定要告诉MJRefresh,请求完成
    // 添加下拉刷新
    MJRefreshNormalHeader *headerView = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    headerView.automaticallyChangeAlpha = YES;
    self.tableView.mj_header = headerView;
    
    // 添加上拉刷新
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    footer.automaticallyHidden = YES;
    self.tableView.mj_footer = footer;
}


- (void)reload {
    // 判断只有显示到屏幕上,才需要刷新
    // 只要当前控件能获取到窗口,就表示显示到屏幕
    if (self.view.window) {
        [self.tableView.mj_header beginRefreshing];
    }
}

#pragma mark - 加载数据
- (void)loadNewData {
    
    // 取消上拉刷新
    [self.mgr.tasks makeObjectsPerformSelector:@selector(cancel)];

    //1.创建管理者
//    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    
    //2.拼接参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    if ([self isKindOfClass:[ZTCareNewsTableViewController class]]) {
        
        parameters[@"appname"] = @"zhibo8";
        parameters[@"platform"] = @"ios";
        parameters[@"version_code"] = @"4.3.0";
        parameters[@"_only_care"] = @"1";
    }else {
        parameters[@"key"] = self.type;//关注新闻界面没有这个参数
        parameters[@"appname"] = @"zhibo8";
        parameters[@"platform"] = @"ios";
        parameters[@"version_code"] = @"4.3.0";
        parameters[@"_only_care"] = @"1";
    }
    
    //3.发送请求
    [self.mgr GET:self.url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        // 结束刷新
        [self.tableView.mj_header endRefreshing];
        
        //1.获取字典数组
        if ([self isKindOfClass:[ZTCareNewsTableViewController class]]) {
            
            NSArray *dictArray = responseObject;
            
            //2.字典数组转模型数组
            _modelArray = [ZTVideoICelltem mj_objectArrayWithKeyValuesArray:dictArray];
            
        }else {
            
            NSArray *dictArray = responseObject[@"video_arr"];
//            NSArray *dictArray = responseObject[@"channel"];
            //2.字典数组转模型数组
            _modelArray = [ZTVideoICelltem mj_objectArrayWithKeyValuesArray:dictArray];
            
        }
        
        //3.刷新表格
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}

#pragma mark - 加载数据
- (void)loadMoreData {
    
    // 取消上拉刷新
    [self.mgr.tasks makeObjectsPerformSelector:@selector(cancel)];
    
    //1.创建管理者
    //    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    
    //2.拼接参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    if ([self isKindOfClass:[ZTCareNewsTableViewController class]]) {
        
        parameters[@"appname"] = @"zhibo8";
        parameters[@"platform"] = @"ios";
        parameters[@"version_code"] = @"4.3.0";
        parameters[@"_only_care"] = @"1";
    }else {
        parameters[@"key"] = self.more_type;//关注新闻界面没有这个参数
        parameters[@"appname"] = @"zhibo8";
        parameters[@"platform"] = @"ios";
        parameters[@"version_code"] = @"4.3.0";
        parameters[@"_only_care"] = @"1";
    }
    
    //3.发送请求
    [self.mgr GET:self.more_url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        // 结束刷新
        [self.tableView.mj_footer endRefreshing];
        
        //1.获取字典数组
        if ([self isKindOfClass:[ZTCareNewsTableViewController class]]) {
            
            NSArray *dictArray = responseObject;
            
            //2.字典数组转模型数组
            NSMutableArray *Array = [ZTVideoICelltem mj_objectArrayWithKeyValuesArray:dictArray];
            
            for (ZTVideoICelltem *item in Array) {
                [self.modelArray addObject:item];
            }
            
        }else {
            
            NSArray *dictArray = responseObject[@"video_arr"];
            //            NSArray *dictArray = responseObject[@"channel"];
            //2.字典数组转模型数组
            NSMutableArray *Array = [ZTVideoICelltem mj_objectArrayWithKeyValuesArray:dictArray];
            
            for (ZTVideoICelltem *item in Array) {
                [self.modelArray addObject:item];
            }
            
        }
        
        //3.刷新表格
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _modelArray.count;
}


- (ZTVideoCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZTVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    cell.item = _modelArray[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}


@end
